package com.example.test2.main.fragment;


import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;

import com.example.test2.R;
import com.example.test2.activity.ClothesActivity;
import com.example.test2.activity.SearchActivity;
import com.example.test2.adapter.MainClothesAdapter;
import com.example.test2.databinding.FragmentBuyerBinding;
import com.example.test2.db.control.ClothesDbController;
import com.example.test2.db.entity.Clothes;
import com.example.test2.event.RoleEvent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

// TODO:  买家首页
public class BuyerFragment extends BaseFragment<FragmentBuyerBinding> implements View.OnClickListener {

    private ClothesDbController clothesDbController;
    private List<Clothes> list = new ArrayList<>();
    private MainClothesAdapter mainClothesAdapter;

    protected void initData() {
        super.initData();
        clothesDbController= ClothesDbController.getInstance(getContext());
        list=clothesDbController.searchAll();

        inAdapter();
        initIndicator();
    }

    @Override
    protected void initClick() {
        super.initClick();
        getBinding().tvSearch.setOnClickListener(this);
        getBinding().tvSell.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_search:
                startActivity(new Intent(getActivity(), SearchActivity.class));
                break;
            case R.id.tv_sell:
                EventBus.getDefault().post(new RoleEvent(false));
                break;
        }
    }

    private void inAdapter() {
        GridLayoutManager manager = new GridLayoutManager(getActivity(), 2);
        manager.setOrientation(GridLayoutManager.VERTICAL);
        getBinding().rvData.setLayoutManager(manager);

        if (mainClothesAdapter==null){
            mainClothesAdapter=new MainClothesAdapter(getActivity(),list);
            getBinding().rvData.setAdapter(mainClothesAdapter);
        }else {
            mainClothesAdapter.setList(list);
            mainClothesAdapter.notifyDataSetChanged();
        }
        mainClothesAdapter.setListener(pos -> {
            Intent intent=new Intent(getActivity(), ClothesActivity.class);
            intent.putExtra("clothes",list.get(pos));
            startActivity(intent);
        });
    }

    private void initIndicator() {
        getBinding().indicatorView.setOnIndicatorChangedListener((oldSelectedIndex, newSelectedIndex) -> {
            list.clear();
            switch (newSelectedIndex){//0-3
                case 0:
                    list=clothesDbController.searchAll();
                    inAdapter();
                    break;
                case 1:
                    list=clothesDbController.searchByType(1+"");
                    inAdapter();
                    break;
                case 2:
                    list=clothesDbController.searchByType(2+"");
                    inAdapter();
                    break;
                case 3:
                    list=clothesDbController.searchByType(3+"");
                    inAdapter();
                    break;
            }
        });
    }

    @Override
    protected int initLayout() {
        return R.layout.fragment_buyer;
    }

    @Override
    protected FragmentBuyerBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) {
        return FragmentBuyerBinding.inflate(inflater, parent, false);
    }

}
